<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/lib/layui-v2.9.8/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	<div class="layui-form-item">
		<h2 align="center">权限新增</h2>
	</div>
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
            <!-- <tip>填写自己管理账号的名称。</tip> -->
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">代码</label>
        <div class="layui-input-block">
			<input type="text" name="code"  placeholder="请输入代码" value="" class="layui-input">
        </div>
    </div>
	
	<div class="layui-form-item layui-form-text">
	    <label class="layui-form-label required">类型</label>
	    <div class="layui-input-block">
	        <input type="radio" name="type_id" value=1 title="目录" lay-filter="powerType" checked="">
	        <input type="radio" name="type_id" value=2 title="菜单" lay-filter="powerType">
			<input type="radio" name="type_id" value=3 title="按钮" lay-filter="powerType">
			<input type="radio" name="type_id" value=4 title="其他" lay-filter="powerType">
	    </div>
	</div>
	
	<div class="layui-form-item">
	    <label class="layui-form-label">父项</label>
	    <div class="layui-input-block">
			<select id="parentID" name="parent_id" lay-search>
			</select>
	        
	    </div>
	</div>
	
    <div class="layui-form-item">
        <label class="layui-form-label">图标值</label>
        <div class="layui-input-block">
            <input type="text" name="icon" placeholder="请输入图标值" value="" class="layui-input">
        </div>
    </div>

	<div class="layui-form-item">
	    <label class="layui-form-label required">排序</label>
	    <div class="layui-input-block">
	        <input type="number" name="sort" lay-verify="required" placeholder="请输入排序序号" value="" class="layui-input">
	    </div>
	</div>
	
	<div class="layui-form-item layui-form-text">
	    <label class="layui-form-label required">是否启用</label>
	    <div class="layui-input-block">
	        <input type="radio" name="enable" value="1" title="启用" checked="">
	        <input type="radio" name="enable" value="0" title="禁用">
	    </div>
	</div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../static/lib/layui-v2.9.8/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
		
		var token = "{{ csrf_token }}";
		$.ajaxSetup({
		    headers: { 
			 'X-CSRFTOKEN': `${token}`,
			 'AUTHORIZATION': 'Bearer ' + localStorage.token,
			 }, // JWTAuthentication.authenticate()方法获取 header = request.META.get(api_settings.AUTH_HEADER_NAME),
		});
		
		$.ajaxSetup({
			complete: function (XMLHttpRequest, textStatus) {
				if (XMLHttpRequest.status === 401) {  // 401 表示未授权，即 token 无效
					layer.msg('授权已过期，需重新登陆', { icon: 2, time: 2000 }, function () {
						window.location.href = '/login';
					})
				}
			}
		});
		
		
		$(function(){   //页面加载时，AJAX请求
			  $.ajax({
				url:'/auth-query',
				type:'post',
				dataType:'json',
				success:function(result){
					console.log(result);
				},
				error:function(){
					layer.msg('授权查询API出错！');
				}
			})
		});
		
		
		//监听单选框
		form.on("radio(powerType)", function (data) {
			let typeValue = $("input[name='type_id']:checked").val();
			console.log(typeValue);
			$.ajax({
				url:'/power-sub-query',
				data:{type_value:typeValue},
				type:'post',
				dataType:'json',
				success:function(result){
					var list = result.data;
					var str = "<option value=''></option>"
					for (var i=0; i<list.length;i++) {
						str += "<option value=\""+list[i].power_id+"\">"+ list[i].power_name+"</option>"
					}
				    console.log(str);
					$('#parentID').html(str);
					form.render('select')   //重新渲染 form select内容
				},
				error:function(){
					layer.msg('部门查询API出错！');
				}
			})
		});

	
        //监听提交
        form.on('submit(saveBtn)', function (data) {
			
			//传给后台
			$.ajax({
				url:"/power/",
				type:"post",
				data:data.field,
				success:function(result){
					if (result.success){
						layer.msg(result.msg, { icon: 1, time: 2000 }, function () {
						  parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
						  parent.layui.table.reload('currentTableId')
						})
					}
					else{
						layer.alert(result.msg);
					}
					
				},
				error:function(result){
					alert("接口错误！！！");
				}
				});
			
			return false;
			
        });

    });
</script>



</body>
</html>